package com.husky.easyexcle.utils;

import com.alibaba.excel.util.StringUtils;
import com.husky.easyexcle.entity.*;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author huanfuan
 * @date 2023/5/29 15:57
 */

@Slf4j
public class ListUtil {

    /**
     *  通过遍历两个List中按组织编号关联匹配
     * @param etlJobEntities List<EtlJobEntity>
     * @param envfileEntityList  List<EnvfileEntity>
     */

    public static List<EtlJobEntity> compareListHitData(List<EtlJobEntity> etlJobEntities, List<EnvfileEntity> envfileEntityList) {
        List<EtlJobEntity> list=etlJobEntities.stream().map(etlJob -> envfileEntityList.stream()
                        .filter(envfile -> Objects.equals(etlJob.getETL_JOB(),envfile.getTaskName()))
                        .findFirst().map(i -> {
                            etlJob.setEnv_file(i.getEnvFileName());
                            return etlJob;
                        }).orElse(null))
                .filter(Objects::nonNull).collect(Collectors.toList());
        return list;
    }


    /**
     *  通过遍历两个List中按组织编号关联匹配
     * @param etlJobEntities List<EtlJobEntity>
     * @param envfileEntityList  List<EnvfileEntity>
     */

    public static List<TaskDefinitionEntity> compareListTaskData(List<TaskDefinitionEntity> etlJobEntities, List<EnvfileEntity> envfileEntityList) {
        List<TaskDefinitionEntity> list=etlJobEntities.stream().map(etlJob -> envfileEntityList.stream()
                        .filter(envfile -> Objects.equals(etlJob.getName(),envfile.getTaskName()))
                        .findFirst().map(i -> {
                            etlJob.setResourceList(etlJob.getResourceList()+","+i.getEnvFileName());
                            return etlJob;
                        }).orElse(null))
                .filter(Objects::nonNull).collect(Collectors.toList());
        return list;
    }



    /*
     *
     * 处理env资源文件关系
     *
     * */
    public static List<TaskDefinitionEntity> compareEnvFile(List<TaskDefinitionEntity> taskDefinitionEntities, List<EnvfileEntity> envfileEntityList){


        List<TaskDefinitionEntity> list = taskDefinitionEntities.stream()
                         .map((uA) -> {
                               return envfileEntityList.stream()
                                         .filter((uB) -> {
                                                return StringUtils.equals(uB.getTaskName(), uA.getName());
                                           })
                                       .map((uB) -> {
                                           uA.setResourceList(uA.getResourceList()+","+uB.getEnvFileName());
                                           uA.setLocalParams(uA.getLocalParams().replace("{2}",uB.getEnvFileName()));
                                           return uA;
                                         })
                                        .collect(Collectors.toList());
                             }) // 结果类型 Steam<List<Hero>>
                         .flatMap(List::stream) // 结果类型 Steam<Hero>
                        .collect(Collectors.toList()); // 结果类型 List<Hero>

        return list;
    }


    /*
     *
     * 处理env资源文件关系
     *
     * */
    public static List<TaskDefinitionEntity> compareNewEnvFile(List<TaskDefinitionEntity> taskDefinitionEntities, List<EnvfileEntity> envfileEntityList){
        List<TaskDefinitionEntity> list = new ArrayList<>();
        for (TaskDefinitionEntity  taskDefinition: taskDefinitionEntities) {
            for (EnvfileEntity envfile : envfileEntityList) {
                if (StringUtils.equals(envfile.getTaskName(), taskDefinition.getName())) {
                    taskDefinition.setResourceList(taskDefinition.getResourceList() + "," + envfile.getEnvFileName());
                    taskDefinition.setLocalParams(taskDefinition.getLocalParams().replace("{2}", envfile.getEnvFileName()));
                }
            }
            list.add(taskDefinition);
        }
        return list;
    }






    /*
     *
     * 处理G04临时表依赖关系
     *
     * */
    public static List<TaskDependentEntity> compareDepData(List<TaskDependentEntity> taskDependentEntities, List<ProcessNameEntity> processNameEntities){


        List<TaskDependentEntity> list = taskDependentEntities.stream()
                .map((uA) -> {
                    return processNameEntities.stream()
                            .filter((uB) -> {
                                return StringUtils.equals(uB.getETL_Job(), uA.getTaskName());
                            })
                            .map((uB) -> {
                                uA.setProcessName(uB.getProcessName());
                                return uA;
                            })
                            .collect(Collectors.toList());
                }) // 结果类型 Steam<List<Hero>>
                .flatMap(List::stream) // 结果类型 Steam<Hero>
                .collect(Collectors.toList()); // 结果类型 List<Hero>

        return list;
    }



    /*
    * 作业流_作业依赖关系
    * G04->G01
    * G07->G04
    * G08->G07
    * */
    public static List<TaskDependentEntity> compareTaskDepData(List<TaskDefinitionEntity> taskDefinitionEntities, List<TaskDependentEntity> taskDependentEntities){


        List<TaskDependentEntity> list = taskDependentEntities.stream()
                .map((uA) -> {
                    return taskDefinitionEntities.stream()
                            .filter((uB) -> {
                                return StringUtils.equals(uB.getName(), uA.getTaskName());
                            })
                            .map((uB) -> {
                                uA.setProcessName(uB.getProcessName());
                                return uA;
                            })
                            .collect(Collectors.toList());
                }) // 结果类型 Steam<List<Hero>>
                .flatMap(List::stream) // 结果类型 Steam<Hero>
                .collect(Collectors.toList()); // 结果类型 List<Hero>

        return list;
    }


    /*
     * 作业流_作业依赖关系
     * G04->G01
     * G07->G04
     * G08->G07
     * */
    public static List<TaskDependentEntity> compareNewTaskDepData(List<TaskDefinitionEntity> taskDefinitionEntities, List<TaskDependentEntity> taskDependentEntities){

        List<TaskDependentEntity> list = new ArrayList<>();
        for (TaskDependentEntity  taskDependent: taskDependentEntities) {
            for (TaskDefinitionEntity taskDefinitionEntity : taskDefinitionEntities) {
                if (StringUtils.equals(taskDependent.getTaskName(), taskDefinitionEntity.getName())) {
                    taskDependent.setProcessName(taskDefinitionEntity.getProcessName());
                }
            }
            list.add(taskDependent);
        }
        return list;

    }










    }
